import { defineStore } from 'pinia'
import { setSessionStore,getSessionStore,setStore,getStore } from '@/utils/store'
const useKnowledgeStore = defineStore("knowledge", {
    state:()=>({
        konwledgeInfo:getSessionStore({name:'konwledgeInfo'}) || {},
        konwledgeAuth:getSessionStore({name:'konwledgeAuth'}) || [],
        enterFolderId:getSessionStore({name:'enterFolderId'}) || {},
        breadPath:getSessionStore({name:'breadPath'}) || {},
        dcLibrary:getSessionStore({name:'dcLibrary'}) || {},
        backPageList: getStore({name:'backPageList',reqEnc:true}) || {}
    }),
    actions:{
        SET_KONWLEDGE_AUTH:( konwledgeAuth)=>{
            this.konwledgeAuth = konwledgeAuth
            setSessionStore({name:'konwledgeInfo',content:this.konwledgeAuth})
        },
        SET_KONWLEDGE: ( konwledgeInfo) => {
            this.konwledgeInfo = JSON.parse(JSON.stringify(konwledgeInfo))
            this.konwledgeInfo.children = []
            setSessionStore({name:'konwledgeInfo',content:this.konwledgeInfo})
        },
        setEnterFolderId(id,knowId){
            this.enterFolderId[knowId] = id
            setSessionStore({name:'enterFolderId',content:this.enterFolderId})
        },
        setBreadPath(val,knowId){
            this.breadPath[knowId] = val
            setSessionStore({name:'breadPath',content:this.breadPath})
        },
        setDcLibrary(val,id){
            this.dcLibrary[id] = val
            setSessionStore({name:'dcLibrary',content:this.dcLibrary})
        },
        setBackPage(val,docId){
            if(this.backPageList[docId]){
                this.backPageList[docId].num +=1
            }else{
                this.backPageList[docId] = {
                    data:{},
                    num:0,
                }
                this.backPageList[docId].data = val
                this.backPageList[docId].num = 0
            }
            setStore({name:'backPageList',content:this.backPageList,reqEnc:true})
        },
        changeBackPage(val,id){
            this.backPageList[id] = val
            setStore({name:'backPageList',content:this.backPageList,reqEnc:true})
        },
        delBackPage(id){
            delete this.backPageList[id]
            setStore({name:'backPageList',content:this.backPageList,reqEnc:true})
        },
        delKnowledge(id){
            delete  this.enterFolderId[id]
            setSessionStore({name:'enterFolderId',content:this.enterFolderId})
            delete  this.breadPath[id]
            setSessionStore({name:'breadPath',content:this.breadPath})
            delete  this.dcLibrary[id]
            setSessionStore({name:'dcLibrary',content:this.dcLibrary})
        },
        clearKnowledge(){
            this.konwledgeInfo = {}
            this.konwledgeAuth = []
            this.enterFolderId = {}
            this.breadPath = {}
            this.dcLibrary = {}
            this.backPageList = {}
        }
    }
})
export default useKnowledgeStore